package com.hx.petsever.controller;


import com.hx.petsever.config.Result;
import com.hx.petsever.entity.User;
import com.hx.petsever.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    public Result Tologin(User user) {
        Map loginMsg = userService.login(user);
        Result<Map> result = new Result<>();
        result.setData(loginMsg);
        if(loginMsg == null) result.fail("账号或密码错误");
        else {
            result.success("登录成功");
        }
        return result;
    }

    @PostMapping("/Register")
    public Result ToRegister(User user)
    {
        Result<Object> result = new Result<>();
        HashMap<String,String> register = userService.Register(user);
        if(register.get("state").equals("0"))
        {
            result.fail(register.get("msg"));
        }else {
            result.success(register.get("msg"));
        }
        return result;
    }

    @PostMapping("/verify")
    public Result ToVerify(
            @Param("password") String password,
            @Param("phone") String phone,
            @Param("openID") String openID) {
        Result<Object> result = new Result<>();
        HashMap<String,String> register = userService.verify(password,phone,openID);
        if(register.get("state").equals("0"))
        {
            result.fail(register.get("msg"));
        }else {
            result.success(register.get("msg"));
        }
        return result;
    }

    @PostMapping("ifverify")
    public Result ToVerify(
            @Param("openID") String openID) {
        Result<Object> result = new Result<>();
        Boolean ifverify = userService.ifverify(openID);
        result.setData(ifverify);
        return result;
    }
}
